package com.etc.utils;


import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;


public class EsUtil {


    private EsUtil(){}

    private static TransportClient client = null;

    public static TransportClient getEs() {
        if (client == null){
            try{
                //设置集群名称
                Settings settings = Settings.builder()
                        .put("cluster.name","my-application")
                        .build();
                //创建client
                client = new PreBuiltTransportClient(settings).addTransportAddresses(
                        //用java访问ES用的端口是9300
                        new InetSocketTransportAddress(InetAddress.getByName("192.168.88.6"), 9300),
                        new InetSocketTransportAddress(InetAddress.getByName("192.168.88.7"), 9300),
                        new InetSocketTransportAddress(InetAddress.getByName("192.168.88.8"), 9300));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return client;
    }
}
